﻿using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using System.IO;

namespace CDTM.Modules.ONLINE.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Online item = Components.Online.Load(this.ItemID);
                item.Delete();

                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Components.Online item = new Components.Online();
            if (this.ItemID > 0)
            {
                item = Components.Online.Load(this.ItemID);
            }

            item.ModuleID = this.ModuleID;

            //NoiHoTro
            item.NoiHoTro = txtNoiHoTro.Text;

            //HoTen
            item.HoTen = txtHoTen.Text.Trim();

            //NickName
            item.NickName = txtNickName.Text.Trim();

            //NickType
            item.NickType = ddlNickType.SelectedValue;

            //SortOrder
            item.SortOrder = txtSortOrder.Text != "" ? int.Parse(txtSortOrder.Text) : 0;

            //Kich hoat
            item.TrangThai = this.chkStatus.Checked;

            //Icon Online/ Offline
            /*
            if (fuImageUrl.HasFile)
            {
                string physicalDirectory = Server.MapPath("~/Uploads/" + this.ModuleID);
                if (Directory.Exists(physicalDirectory) == false)
                {
                    Directory.CreateDirectory(physicalDirectory);
                }
                string physicalPath = physicalDirectory + "\\" + this.fuImageUrl.FileName;
                int i = 1;
                while (File.Exists(physicalPath))
                {
                    physicalPath = physicalDirectory + "\\" + i + "_" + this.fuImageUrl.FileName;
                    i++;
                }

                fuImageUrl.SaveAs(physicalPath);
                item.ImageUrl = "~/Uploads/" + this.ModuleID + "/" + this.fuImageUrl.FileName;
            }
            */

            //Insert item
            if (this.ItemID == 0)
            {
                this.ItemID = item.Insert();
            }
            else
            {
                item.ID = this.ItemID;
                item.Update();
            }

            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.ItemID > 0)
                {
                    Components.Online item = Components.Online.Load(this.ItemID);
                    if (item != null)
                    {
                        txtNoiHoTro.Text = item.NoiHoTro;
                        txtHoTen.Text = item.HoTen;
                        txtNickName.Text = item.NickName;
                        txtSortOrder.Text = item.SortOrder.ToString();
                        chkStatus.Checked = item.TrangThai;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}